package com.timber.panels;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;

import pv.jfcx.JPVCurrency;
import pv.jfcx.JPVDatePlus;
import pv.jfcx.JPVEdit;

import com.borland.jbcl.layout.VerticalFlowLayout;
import com.timber.frame.ImsFrame;
import com.timber.util.Global;
import com.timber.util.ITPEdit;
import com.timber.util.ImageMgr;
import com.timber.util.InvetoryBean;
import com.timber.util.SearchableComboBox;
import com.timber.util.TimberNameMgr;
import com.timber.util.TimberdetailMgr;

public class ItemInfoPanel extends JPanel{
	protected ImsFrame parent;
	protected GridBagLayout gridBagLayout1 = new GridBagLayout();
	protected GridBagLayout gridBagLayout2 = new GridBagLayout();
	protected JPVCurrency LengthTF = new JPVCurrency();
	protected JLabel LengthLabel; 
	protected TitledBorder		titledBorder;
	protected ITPEdit itemCodeTF = new ITPEdit();
	protected SearchableComboBox ItemNameTF =new SearchableComboBox( Global.getMainFrame(),
    		TimberNameMgr.mgr.getDataVector(),
            SearchableComboBox.INCLUDE_NONE,
            true );
	protected SearchableComboBox ItemClassTF = new SearchableComboBox( Global.getMainFrame(),
    		TimberNameMgr.mgr.getDataVector(),
            SearchableComboBox.INCLUDE_NONE,
            true );
	protected JLabel ItemLabel; 
	protected JLabel			timberNameLabel = new JLabel();
	protected JLabel			classLabel = new JLabel();
	protected JPVCurrency taxTF = new JPVCurrency();
	protected JLabel taxLabel; 
	protected JPVCurrency widthTF = new JPVCurrency();
	protected JLabel 		widthLabel;
	protected JPVCurrency qtyTF = new JPVCurrency();
	protected JLabel 		qtyLabel;
	protected JPVCurrency ProfitMarginTF = new JPVCurrency();
	protected JLabel 	totalLabel;
	protected JPVCurrency GrandTotalTF = new JPVCurrency();
	protected JLabel GrandTotalLabel; 
	protected JPanel			woodPanel = new JPanel();
	protected JPanel			topPanel = new JPanel();
	protected JPanel			topPanel2 = new JPanel();
	protected JPanel			topPanel3 = new JPanel();
	protected VerticalFlowLayout		verticalFlowLayout1 = new VerticalFlowLayout();
	protected GridBagLayout	 gridBagLayout3=new GridBagLayout();
	protected Component			hstrut =  Box.createRigidArea(new Dimension(280,20));
	protected JButton			addBtn = new JButton();
	protected ImageIcon			addImage;
	TimberdetailMgr objTimberdetailMgr=new TimberdetailMgr();
	Vector currenttimber= new Vector();
	protected Color					focusBG;
	protected Color					nonFocusBG;
	
	protected JLabel			MeasurementLabel = new JLabel();
	protected JLabel			PurchasePriceLabel = new JLabel();
	protected JLabel			SalesPriceLabel = new JLabel();
	protected JLabel			ProfitMarginLabel = new JLabel();
	protected JLabel			PurchasedFromLabel = new JLabel();
	protected JLabel			PurchasedDate = new JLabel();
	protected JLabel			Location = new JLabel();
	protected JLabel			LotNumber = new JLabel();
	
	protected ITPEdit			MeasurementTextBox = new ITPEdit();
	protected ITPEdit			PurchasePriceTextBox = new ITPEdit();
	protected ITPEdit			SalesPriceTextBox = new ITPEdit();
	protected ITPEdit			ProfitMarginTextBox = new ITPEdit();
	protected ITPEdit			PurchasedFromTextBox = new ITPEdit();
	protected JPVDatePlus		PurchasedDateTextBox = new  JPVDatePlus();
	protected ITPEdit			LocationTextBox = new ITPEdit();
	protected ITPEdit			LotNumberTextBox = new ITPEdit();
	
	
	public ItemInfoPanel(ImsFrame parent, boolean showExtraButtons) {
		this.parent = parent;
		
		try {
			jbInit();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
	
	void jbInit() throws Exception {
		
		this.setLayout(verticalFlowLayout1);
		this.setBackground(new Color(204, 204, 204));
		//this.setMinimumSize(new Dimension(650, 197));    
		//this.setPreferredSize(new Dimension(650, 150)); 
		Dimension d = new Dimension(80,20);
		titledBorder = new TitledBorder( BorderFactory.createEtchedBorder(), " Qty" );
		
		addImage       = ImageMgr.getIcon( ImageMgr.PLUS_ICON );
		topPanel.setLayout( gridBagLayout1 );
		//topPanel.setPreferredSize(new Dimension(150, 50));
		topPanel2.setLayout( gridBagLayout3 );
		woodPanel.setLayout( gridBagLayout2 );
		
		topPanel3.setBorder( titledBorder );
		ItemLabel  = new JLabel("Timber Code");
		LengthLabel = new JLabel("Length");
		widthLabel = new JLabel("Width"); 
		taxLabel  = new JLabel("Tax");
		qtyLabel=new JLabel("Cubic Qty");
		totalLabel=new JLabel("Total");
		GrandTotalLabel=new JLabel("GrandTotal");
		classLabel.setText( "Timber Type" );
		timberNameLabel.setText( "Timber Name" );
		MeasurementLabel .setText("Measurement");
		PurchasePriceLabel .setText("PurchasePrice");
		SalesPriceLabel .setText("SalesPrice");
		ProfitMarginLabel .setText("ProfitMargin");
		PurchasedFromLabel .setText("PurchasedFrom");
		PurchasedDate .setText("PurchasedDate");
		Location .setText("Location");
		LotNumber .setText("LotNumber");
		ItemNameTF.setName("Timber Name");
		
		itemCodeTF.setMaxLength( 9 );
		itemCodeTF.setCase( JPVEdit.UPPER );
		itemCodeTF.setIncludeCharacters( "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" );
		
		/*
		ItemNameTF.setMaxLength( 9 );
		ItemNameTF.setCase( JPVEdit.UPPER );
		ItemNameTF.setIncludeCharacters( "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" );
		
		ItemClassTF.setMaxLength( 9 );
		ItemClassTF.setCase( JPVEdit.UPPER );
		ItemClassTF.setIncludeCharacters( "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" );
		*/
		
		MeasurementTextBox.setMaxLength( 9 );
		MeasurementTextBox.setCase( JPVEdit.UPPER );
		MeasurementTextBox.setIncludeCharacters( "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" );
		
		PurchasePriceTextBox.setMaxLength( 9 );
		PurchasePriceTextBox.setCase( JPVEdit.UPPER );
		PurchasePriceTextBox.setIncludeCharacters( "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" );
		
		SalesPriceTextBox.setMaxLength( 9 );
		SalesPriceTextBox.setCase( JPVEdit.UPPER );
		SalesPriceTextBox.setIncludeCharacters( "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" );
		
		ProfitMarginTextBox.setMaxLength( 9 );
		ProfitMarginTextBox.setCase( JPVEdit.UPPER );
		ProfitMarginTextBox.setIncludeCharacters( "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" );
		
		PurchasedFromTextBox.setMaxLength( 9 );
		PurchasedFromTextBox.setCase( JPVEdit.UPPER );
		PurchasedFromTextBox.setIncludeCharacters( "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" );
		
		PurchasedDateTextBox.setMaxLength( 9 );
		PurchasedDateTextBox.setCase( JPVEdit.UPPER );
		PurchasedDateTextBox.setIncludeCharacters( "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" );
		
		LocationTextBox.setMaxLength( 9 );
		LocationTextBox.setCase( JPVEdit.UPPER );
		LocationTextBox.setIncludeCharacters( "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" );
		
		LotNumberTextBox.setMaxLength( 9 );
		LotNumberTextBox.setCase( JPVEdit.UPPER );
		LotNumberTextBox.setIncludeCharacters( "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" );
		
		
		
		LengthTF.setDecimals(4);
		LengthTF.setSymbol("");
		LengthTF.setText("");        
		LengthTF.setPreferredSize(d);
		LengthTF.setMinimumSize(d);
		LengthTF.setAlignment(2);
		
		LengthTF.addFocusListener(new java.awt.event.FocusAdapter() {
			
			public void focusLost(FocusEvent e) {
				
				calQty();
				calTotalandTax();
				
			}
		});
		
		
		widthTF.setDecimals(4);
		widthTF.setSymbol("");
		widthTF.setText("");        
		widthTF.setPreferredSize(d);
		widthTF.setMinimumSize(d);
		widthTF.setAlignment(2);
		
		widthTF.addFocusListener(new java.awt.event.FocusAdapter() {
			
			public void focusLost(FocusEvent e) {
				
				calQty();
				calTotalandTax();
				
			}
		});
		
		
		
		taxTF.setSymbol("");
		taxTF.setDecimals(4);
		taxTF.setSymbol("");
		taxTF.setText("");        
		taxTF.setPreferredSize(d);
		taxTF.setMinimumSize(d);
		taxTF.setAlignment(2);
		taxTF.setEditable(false);
		
		ProfitMarginTF.setDecimals(4);
		ProfitMarginTF.setSymbol("");
		ProfitMarginTF.setText("");        
		ProfitMarginTF.setPreferredSize(d);
		ProfitMarginTF.setMinimumSize(d);
		ProfitMarginTF.setAlignment(2);
		ProfitMarginTF.addFocusListener(new java.awt.event.FocusAdapter() {
			
			public void focusLost(FocusEvent e) {
				calTotalandTax();
				
			}
		});
		
		qtyTF.setDecimals(4);
		qtyTF.setSymbol("");
		qtyTF.setText("");        
		qtyTF.setPreferredSize(d);
		qtyTF.setMinimumSize(d);
		qtyTF.setAlignment(2);
		qtyTF.setEditable(false);
		
		
		addBtn.setIcon( addImage );
		addBtn.setMargin( new Insets( 0, 0, 0, 0 ) );
		addBtn.addActionListener( new ActionListener()
				{
			public void actionPerformed( ActionEvent e )
			{
				System.out.println("Clicked Add Button");
			}
			
			
				} );
		
		// item TF action performed
		
		itemCodeTF.setMaxLength( 9 );
		itemCodeTF.setCase( JPVEdit.UPPER );
		itemCodeTF.setIncludeCharacters( "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" );
		itemCodeTF.addFocusListener(new java.awt.event.FocusAdapter() {
			public void focusGained(FocusEvent e) {
				if (focusBG != null)
				{
					//itemTF.setBackground(focusBG);
					System.out.println("Focuse gained ");
				}
			}
			public void focusLost(FocusEvent e) {
				{
					InvetoryBean objInvetoryBean=null;
					currenttimber=objTimberdetailMgr.getTimberDetails(itemCodeTF.getText());
					if(currenttimber.size()>0)
					{
						objInvetoryBean=(InvetoryBean)currenttimber.get(0);
						//ItemClassTF.setText(objInvetoryBean.getTClass());
						//ItemNameTF.setText(objInvetoryBean.getTimberName());
						ProfitMarginTF.setDouble(objInvetoryBean.getSalesRate());
					}
					
				}
			}
		});
		
		topPanel3.setLayout(verticalFlowLayout1);
		this.add(topPanel3, null);
		
		topPanel.add( ItemLabel,
				new GridBagConstraints( 2,
						0,
						1,
						1,
						0.0,
						0.0,
						GridBagConstraints.SOUTHWEST,
						GridBagConstraints.NONE,
						new Insets( 10, 5, 0, 0 ), 0, 0 ) );
		topPanel.add( itemCodeTF,
				new GridBagConstraints( 2,
						1,
						1,
						1,
						0.0,
						0.0,
						GridBagConstraints.NORTHWEST,
						GridBagConstraints.NONE,
						new Insets( 0, 5, 0, 0 ), 0, 0 ) );
	
		
		topPanel.add( timberNameLabel,
				new GridBagConstraints( 3,
						0,
						1,
						1,
						0.0,
						0.0,
						GridBagConstraints.SOUTHWEST,
						GridBagConstraints.NONE,
						new Insets( 10, 15, 0, 0 ), 0, 0 ) );
		topPanel.add( ItemNameTF,
				new GridBagConstraints( 3,
						1,
						1,
						1,
						0.0,
						0.0,
						GridBagConstraints.NORTHWEST,
						GridBagConstraints.NONE,
						new Insets( 0, 5, 0, 0 ), 0, 0 ) );
		topPanel.add( classLabel,
				new GridBagConstraints( 4,
						0,
						1,
						1,
						0.0,
						0.0,
						GridBagConstraints.SOUTHWEST,
						GridBagConstraints.NONE,
						new Insets( 10, 15, 0, 0 ), 0, 0 ) );
		topPanel.add( ItemClassTF,
				new GridBagConstraints( 4,
						1,
						1,
						1,
						0.0,
						0.0,
						GridBagConstraints.NORTHWEST,
						GridBagConstraints.NONE,
						new Insets( 0, 5, 0, 0 ), 0, 0 ) );
		
		
		//TODO
		
		topPanel.add( LengthLabel,
				new GridBagConstraints( 5,
						0,
						1,
						1,
						0.0,
						0.0,
						GridBagConstraints.SOUTHWEST,
						GridBagConstraints.NONE,
						new Insets( 10, 15, 0, 0 ), 0, 0 ) );
		topPanel.add( LengthTF,
				new GridBagConstraints( 5,
						1,
						1,
						1,
						0.0,
						0.0,
						GridBagConstraints.NORTHWEST,
						GridBagConstraints.NONE,
						new Insets( 0, 5, 0, 0 ), 0, 0 ) );
		
		
		topPanel.add( widthLabel,
				new GridBagConstraints( 6,
						0,
						1,
						1,
						0.0,
						0.0,
						GridBagConstraints.SOUTHWEST,
						GridBagConstraints.NONE,
						new Insets( 10, 15, 0, 0 ), 0, 0 ) );
		topPanel.add( widthTF,
				new GridBagConstraints( 6,
						1,
						1,
						1,
						0.0,
						0.0,
						GridBagConstraints.NORTHWEST,
						GridBagConstraints.NONE,
						new Insets( 0, 5, 0, 0 ), 0, 0 ) );
		
		
		topPanel.add( qtyLabel,
				new GridBagConstraints( 7,
						0,
						1,
						1,
						0.0,
						0.0,
						GridBagConstraints.SOUTHWEST,
						GridBagConstraints.NONE,
						new Insets( 10, 15, 0, 0 ), 0, 0 ) );
		topPanel.add( qtyTF,
				new GridBagConstraints(7 ,
						1,
						1,
						1,
						0.0,
						0.0,
						GridBagConstraints.NORTHWEST,
						GridBagConstraints.NONE,
						new Insets( 0, 5, 0, 0 ), 0, 0 ) );
		
		
		
		topPanel.add( MeasurementLabel,
				new GridBagConstraints( 8,
						0,
						1,
						1,
						0.0,
						0.0,
						GridBagConstraints.SOUTHWEST,
						GridBagConstraints.NONE,
						new Insets( 10, 15, 0, 0 ), 0, 0 ) );
		topPanel.add( MeasurementTextBox,
				new GridBagConstraints(8 ,
						1,
						1,
						1,
						0.0,
						0.0,
						GridBagConstraints.NORTHWEST,
						GridBagConstraints.NONE,
						new Insets( 0, 5, 0, 0 ), 0, 0 ) );
		
		
		
		topPanel.add( PurchasePriceLabel,
				new GridBagConstraints( 9,
						0,
						1,
						1,
						0.0,
						0.0,
						GridBagConstraints.SOUTHWEST,
						GridBagConstraints.NONE,
						new Insets( 10, 15, 0, 0 ), 0, 0 ) );
		topPanel.add( PurchasePriceTextBox,
				new GridBagConstraints(9 ,
						1,
						1,
						1,
						0.0,
						0.0,
						GridBagConstraints.NORTHWEST,
						GridBagConstraints.NONE,
						new Insets( 0, 5, 0, 0 ), 0, 0 ) );
		
		
		topPanel.add( SalesPriceLabel,
				new GridBagConstraints( 10,
						0,
						1,
						1,
						0.0,
						0.0,
						GridBagConstraints.SOUTHWEST,
						GridBagConstraints.NONE,
						new Insets( 10, 15, 0, 0 ), 0, 0 ) );
		topPanel.add( SalesPriceTextBox,
				new GridBagConstraints(10 ,
						1,
						1,
						1,
						0.0,
						0.0,
						GridBagConstraints.NORTHWEST,
						GridBagConstraints.NONE,
						new Insets( 0, 5, 0, 0 ), 0, 0 ) );

		topPanel2.add( hstrut,
				 new GridBagConstraints( 1,
				 0,
				 3,
				 2,
				 0.0,
				 0.0,
				 GridBagConstraints.SOUTHWEST,
				 GridBagConstraints.NONE,
				 new Insets( 10, 15, 0, 0 ), 0, 0 ) );
				 
		 
		topPanel2.add( LotNumber,
				new GridBagConstraints( 4,
						0,
						1,
						1,
						0.0,
						0.0,
						GridBagConstraints.SOUTHWEST,
						GridBagConstraints.NONE,
						new Insets( 12, 10, 0, 0 ), 0, 0 ) );
		topPanel2.add( LotNumberTextBox,
				new GridBagConstraints( 4,
						1,
						1,
						1,
						0.0,
						0.0,
						GridBagConstraints.NORTHWEST,
						GridBagConstraints.NONE,
						new Insets( 0, 10, 0, 0 ), 0, 0 ) );
		topPanel2.add( Location,
				new GridBagConstraints( 5,
						0,
						1,
						1,
						0.0,
						0.0,
						GridBagConstraints.SOUTHWEST,
						GridBagConstraints.NONE,
						new Insets( 12, 10, 0, 0 ), 0, 0 ) );
		topPanel2.add( LocationTextBox,
				new GridBagConstraints( 5,
						1,
						1,
						1,
						0.0,
						0.0,
						GridBagConstraints.NORTHWEST,
						GridBagConstraints.NONE,
						new Insets( 0, 10, 0, 0 ), 0, 0 ) );
		
		
		
		topPanel2.add( PurchasedFromLabel,
				new GridBagConstraints( 6,
						0,
						1,
						1,
						0.0,
						0.0,
						GridBagConstraints.SOUTHWEST,
						GridBagConstraints.NONE,
						new Insets( 12, 10, 0, 0 ), 0, 0 ) );
		topPanel2.add( PurchasedFromTextBox,
				new GridBagConstraints( 6,
						1,
						1,
						1,
						0.0,
						0.0,
						GridBagConstraints.NORTHWEST,
						GridBagConstraints.NONE,
						new Insets( 0, 10, 0, 0 ), 0, 0 ) );
		
		
		
		topPanel2.add( PurchasedDate,
				new GridBagConstraints( 7,
						0,
						1,
						1,
						0.0,
						0.0,
						GridBagConstraints.SOUTHWEST,
						GridBagConstraints.NONE,
						new Insets( 12, 10, 0, 0 ), 0, 0 ) );
		topPanel2.add( PurchasedDateTextBox,
				new GridBagConstraints( 7,
						1,
						1,
						1,
						0.0,
						0.0,
						GridBagConstraints.NORTHWEST,
						GridBagConstraints.NONE,
						new Insets( 0, 10, 0, 0 ), 0, 0 ) );
		
		
		
		// old
		topPanel2.add( ProfitMarginLabel,
				new GridBagConstraints( 8,
						0,
						1,
						1,
						0.0,
						0.0,
						GridBagConstraints.SOUTHWEST,
						GridBagConstraints.NONE,
						new Insets( 12, 10, 0, 0 ), 0, 0 ) );
		topPanel2.add( ProfitMarginTF,
				new GridBagConstraints( 8,
						1,
						1,
						1,
						0.0,
						0.0,
						GridBagConstraints.NORTHWEST,
						GridBagConstraints.NONE,
						new Insets( 0, 10, 0, 0 ), 0, 0 ) );
		
		
		topPanel2.add( taxLabel,
				new GridBagConstraints( 9,
						0,
						1,
						1,
						0.0,
						0.0,
						GridBagConstraints.SOUTHWEST,
						GridBagConstraints.NONE,
						new Insets( 12, 10, 0, 0 ), 0, 0 ) );
		topPanel2.add( taxTF,
				new GridBagConstraints( 9,
						1,
						1,
						1,
						0.0,
						0.0,
						GridBagConstraints.NORTHWEST,
						GridBagConstraints.NONE,
						new Insets( 0, 10, 0, 0 ), 0, 0 ) );
				
		topPanel3.add( topPanel,
				new GridBagConstraints(10,
						1,
						1,
						1,
						0.0,
						0.0,
						GridBagConstraints.NORTHWEST,
						GridBagConstraints.NONE,
						new Insets( 0, 10, 0, 0 ), 0, 0 ) );
		
		topPanel3.add( topPanel2,
				new GridBagConstraints(10,
						1,
						1,
						1,
						0.0,
						0.0,
						GridBagConstraints.NORTHWEST,
						GridBagConstraints.NONE,
						new Insets( 0, 10, 0, 0 ), 0, 0 ) );
		
		
		
		
	}
	
	
	public ItemInfoPanel(ImsFrame parent) {
		this.parent = parent;
		try {
			jbInit();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
	
	public double calQty()
	{
		double qty=0;
		// constant for converting to cubic , it should read from property file 
		double cubiconstant=2;
		qty=LengthTF.getDouble()*widthTF.getDouble()*cubiconstant;	 
		qtyTF.setDouble(qty);
		return qty;
	}
	
	
	
	public double calTotalandTax()
	{
		double qty=0;
		double toatl=0;
		// should get from property file
		double taxconsant=0.12;
		qty=calQty();
		toatl=qty*ProfitMarginTF.getDouble();
		//totalTF.setDouble(toatl);
		taxTF.setDouble(toatl*taxconsant);
		return toatl;
	}
	
	
	public double calamount()
	{
		double amount=0;
		double toatl=0;
		// should get from property file
		double taxconsant=0.12;
		toatl=calTotalandTax();
		amount=toatl+(toatl*taxconsant);
		System.out.println("Total  :"+toatl);
		System.out.println("Tax :"+toatl*taxconsant);
		System.out.println("amount ---------:"+amount);
		return amount;
	}
	
	public void clear()
	{
		itemCodeTF.setText("");
		//ItemNameTF.setText(""); 
		//ItemClassTF.setText("");
		taxTF.setText("");  
		widthTF.setText("");
		qtyTF.setText("");
		ProfitMarginTF.setText(""); 
		//totalTF.setText("");
		LengthTF.setText("");
	}
	
	
	
}
